﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DAL.DBUtility;
using Model.Sys;
using Common;
using Enums;
using Enums.Sys;
namespace DAL.Sys
{
    public class Sys_RoleDAl
    {
        /// 公共Model
        /// </summary>
        /// <param name="sdr"></param>
        /// <returns></returns>
        private Sys_RoleModel CommonModel(SqlDataReader sdr)
        {
            Sys_RoleModel model = new Sys_RoleModel();
            model.ID = PageValidate.ConvertToTrimInt(sdr["ID"]);
            model.ViewDel = PageValidate.ConvertToTrimInt(sdr["ViewDel"]);
            model.ViewFlag = PageValidate.ConvertToTrimInt(sdr["ViewFlag"]);
            model.EnterpriseId = PageValidate.ConvertToTrimInt(sdr["EnterpriseId"]);
            model.RoleName = PageValidate.ConvertToTrimString(sdr["RoleName"]);
            model.MenuId = PageValidate.ConvertToTrimString(sdr["MenuId"]);
            model.Remark = PageValidate.ConvertToTrimString(sdr["Remark"]);
            model.Sequence = PageValidate.ConvertToTrimInt(sdr["Sequence"]);
            model.CreateLoginID = PageValidate.ConvertToTrimInt(sdr["CreateLoginID"]);
            model.CreateLoginName = PageValidate.ConvertToTrimString(sdr["CreateLoginName"]);
            model.CreateDate = PageValidate.ConvertToDateTime(sdr["CreateDate"]);
            model.ChangeLoginID = PageValidate.ConvertToTrimInt(sdr["ChangeLoginID"]);
            model.ChangeLoginName = PageValidate.ConvertToTrimString(sdr["ChangeLoginName"]);
            model.ChangeDate = PageValidate.ConvertToDateTime(sdr["ChangeDate"]);
            model.RoleType = PageValidate.ConvertToTrimInt(sdr["RoleType"]);
            //model.Name = PageValidate.ConvertToTrimString(sdr["Name"]);
            model.OrganizationTypes = PageValidate.ConvertToTrimString(sdr["OrganizationTypes"]);
            return model;
        }

        public List<Sys_RoleModel> Getlist(string str) {
            string sql = @"  select * from Sys_Role where ViewDel=@ViewDel "+str;
            //if (uId != 1) sql += @"and EnterpriseId=@EnterpriseId";
            SqlParameter[] parameters = {
                                            new SqlParameter("@ViewDel", EnumManagement.GetEnumValue(SysEnum.NoDele)),
					                      //  new SqlParameter("@EnterpriseId", enterId)
			};
            return ListDataSet.DataSetToIList<Sys_RoleModel>(DbHelperSQL.Query(sql, parameters)).ToList();
        }

      
        public Sys_RoleModel GetModelById(int id) {
            string sql = @"select * from Sys_Role where ID=@ID";
            SqlParameter[] parameters = {
					new SqlParameter("@ID",id)
			};
            SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parameters);
            Sys_RoleModel model = new Sys_RoleModel();
            if (sdr.Read())
            {
                model = CommonModel(sdr);
            }
            sdr.Close();
            return model;
        }
        public ResultEntity AddRole(Sys_RoleModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Sys_Role(ViewDel,ViewFlag,EnterpriseId,RoleName,MenuId,Remark,Sequence,CreateLoginID,CreateLoginName,CreateDate,ChangeLoginID,ChangeLoginName,ChangeDate,RoleType,OrganizationTypes)");
            strSql.Append(" values(@ViewDel,@ViewFlag,@EnterpriseId,@RoleName,@MenuId,@Remark,@Sequence,@CreateLoginID,@CreateLoginName,@CreateDate,@ChangeLoginID,@ChangeLoginName,@ChangeDate,@RoleType,@OrganizationTypes) ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                            new SqlParameter("@ViewDel",Convert.ToInt32(SysEnum.NoDele)),
                            new SqlParameter("@ViewFlag", Convert.ToInt32(SysEnum.Enable)),
                            new SqlParameter("@EnterpriseId", PageValidate.ConvertToTrimInt(model.EnterpriseId)),
                            new SqlParameter("@RoleName", PageValidate.ConvertToTrimString(model.RoleName)),
                            new SqlParameter("@MenuId", PageValidate.ConvertToTrimString(model.MenuId)),
                            new SqlParameter("@Remark",PageValidate.ConvertToTrimString(model.Remark)),
                            new SqlParameter("@Sequence", PageValidate.ConvertToTrimInt(model.Sequence)),
                            new SqlParameter("@CreateLoginID", PageValidate.ConvertToTrimInt(model.CreateLoginID)),
                            new SqlParameter("@CreateLoginName",PageValidate.ConvertToTrimString(model.CreateLoginName)),
                            new SqlParameter("@CreateDate",PageValidate.ConvertToDateTime(model.CreateDate)),
                            new SqlParameter("@ChangeLoginID", PageValidate.ConvertToTrimInt(model.ChangeLoginID)),
                            new SqlParameter("@ChangeLoginName",PageValidate.ConvertToTrimString(model.ChangeLoginName)),
                            new SqlParameter("@ChangeDate",PageValidate.ConvertToDateTime(model.ChangeDate)),
                            new SqlParameter("@RoleType",PageValidate.ConvertToTrimInt(model.RoleType)),
                            new SqlParameter("@OrganizationTypes",PageValidate.ConvertToTrimString(model.OrganizationTypes)),

                  };
            return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(strSql.ToString(), parameters));
        }

        public ResultEntity UpdRole(Sys_RoleModel model)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"UPDATE [dbo].[Sys_Role]
                                           SET 
                                              [EnterpriseId] =@EnterpriseId		
                                              ,[RoleName] =@RoleName				
                                              ,[Remark] =@Remark
                                        
                                              ,[OrganizationTypes] =@OrganizationTypes
                                              ,[ChangeLoginID] =@ChangeLoginID		
                                              ,[ChangeLoginName] =@ChangeLoginName
                                              ,[ChangeDate] =GETDATE()		
                                               WHERE ID=@ID;");

            SqlParameter[] parameters = {
                            new SqlParameter("@EnterpriseId",PageValidate.ConvertToTrimInt(model.EnterpriseId)),
                            new SqlParameter("@RoleName",PageValidate.ConvertToTrimString(model.RoleName)),
                            new SqlParameter("@Remark",PageValidate.ConvertToTrimString(model.Remark)),
                            //new SqlParameter("@MenuId",PageValidate.ConvertToTrimString(model.MenuId)),
                            new SqlParameter("@OrganizationTypes",PageValidate.ConvertToTrimString(model.OrganizationTypes)),
                            new SqlParameter("@ChangeLoginID",PageValidate.ConvertToTrimInt(model.ChangeLoginID )),
                            new SqlParameter("@ChangeLoginName",PageValidate.ConvertToTrimString(model.ChangeLoginName)),
                            new SqlParameter("@ID",PageValidate.ConvertToTrimInt(model.ID))
                  };
            return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql.ToString(), parameters));
        }

        /// <summary>
        /// 修改指定角色对应的菜单权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResultEntity UpMenu(Sys_RoleModel model)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"UPDATE [dbo].[Sys_Role]
                                           SET 
                                              [MenuId] =@MenuId		
                                               	
                                               WHERE ID=@ID;");

            SqlParameter[] parameters = {
                            new SqlParameter("@MenuId",PageValidate.ConvertToTrimString(model.MenuId.TrimEnd(','))),
                            
                            new SqlParameter("@ID",PageValidate.ConvertToTrimInt(model.ID))
                  };
            return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql.ToString(), parameters));
        }
    }
}
